package jeons.Basket.action;







import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import javax.servlet.http.HttpServletRequest;

import jeons.Basket.biz.BasketBiz;
import jeons.Basket.biz.DealInfoBiz;
import jeons.Basket.biz.DeliverBiz;
import jeons.Basket.biz.PayBiz;


import jeons.Basket.dto.BasketDTO;
import jeons.Basket.dto.DealInfoDTO;
import jeons.Basket.dto.DeliveryDTO;
import jeons.Basket.dto.PayDTO;
import jeons.member.biz.MemberBiz;
import jeons.member.dto.MemberDTO;

import org.springbyexample.web.servlet.view.tiles2.TilesUrlBasedViewResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import sun.swing.BakedArrayList;

@Controller
@RequestMapping("/basket")
public class BasketAction {
	@Autowired TilesUrlBasedViewResolver tilesUrlBasedViewResolver;
	@Autowired BasketBiz basketBiz;
	@Autowired MemberBiz memberBiz;
	@Autowired DealInfoBiz dealInfoBiz;
	@Autowired PayBiz payBiz;
	@Autowired DeliverBiz deliverBiz;
	
	
	@RequestMapping(value="/basket_index", method=RequestMethod.POST)
	public @ResponseBody String basket_Index(@ModelAttribute("formData")BasketDTO basketDTO){
		tilesUrlBasedViewResolver.setTilesDefinitionName("default");
		basketBiz.insertData(basketDTO);
		String check;
		check = "true";
		return check;
	}
	
	
	/**
	* @Method Name  : basket_user
	* @�����  : 2012. 2. 2.
	* @�����  : user
	* @蹂�꼍�대�  :
	* @Method �ㅻ� : �λ�援щ� ��낫
	* @param u_id
	* @param model
	* @return
	*/
	@RequestMapping(value="/basket_user", method=RequestMethod.GET)
	public String basket_user(@RequestParam("u_id")String u_id,Model model){
		System.out.println(u_id);
		model.addAttribute("data", basketBiz.getList(u_id));
		return "basket/basket_user";
	}
	
	
	/**
	* @Method Name  : info_bd
	* @�����  : 2012. 2. 2.
	* @�����  : user
	* @蹂�꼍�대�  :
	* @Method �ㅻ� : �λ�援щ� �����낫
	* @param basketno
	* @param model
	* @return
	*/
	@RequestMapping(value="/info_bd", method=RequestMethod.GET)
	public String info_bd(@RequestParam("basketno")int basketno,Model model){
		tilesUrlBasedViewResolver.setTilesDefinitionName("tmpl");
		model.addAttribute("data",basketBiz.getData(basketno));
		return "basket/basket_info";
	}
	
	
	/**
	* @Method Name  : pay_Index
	* @�����  : 2012. 2. 3.
	* @�����  : user
	* @蹂�꼍�대�  :
	* @Method �ㅻ� : 諛곗� 諛�寃곗�
	* @param chk
	* @param u_id
	* @param model
	* @return
	*/
	@RequestMapping(value="/user_pay", method=RequestMethod.POST)
	public String pay_Index(@RequestParam("chk")Integer[] chk,@RequestParam("u_id")String u_id,Model model){
		tilesUrlBasedViewResolver.setTilesDefinitionName("tmpl");
		System.out.println("aaaaaaaaaaaaaaaaaaa");
		model.addAttribute("data",basketBiz.getPriceSUm(chk));
		model.addAttribute("userdata", memberBiz.getDataUser(u_id));
		MemberDTO kk = memberBiz.getDataUser(u_id);
		return "basket/b_delnpay";
	}
	
	
	
	/** 
	* @Method Name  : user_pay_Ok 
	* @�����  : 2012. 2. 4. 
	* @�����  : Administrator 
	* @蹂�꼍�대�  : 
	* @Method �ㅻ� : 寃곗����吏�
	* @param model
	* @param goods_price_result
	* @param goods_name_result
	* @return 
	*/
	@RequestMapping(value="/user_pay_Ok", method=RequestMethod.GET)
	public String user_pay_Ok(){
		return "basket/user_pay_Ok";
	}
	
	/**
	* @Method Name  : test
	* @�����  : 2012. 2. 6.
	* @�����  : user
	* @蹂�꼍�대�  :
	* @Method �ㅻ� : 결제
	* @param point_write
	* @param basketDTO
	* @param chk
	* @param u_id
	* @param deliveryDTO
	* @param payDTO
	* @return
	*/
	@RequestMapping(value="/basket_update", method=RequestMethod.POST)
	public String test(@RequestParam("point_write")Integer point_write,@ModelAttribute("basket_info")BasketDTO basketDTO,@RequestParam("chk")Integer[] chk,@RequestParam("u_id")String u_id,@ModelAttribute("delivery")DeliveryDTO deliveryDTO,@ModelAttribute("pay_ok")PayDTO payDTO){
	DealInfoDTO dealInfoDTO = new DealInfoDTO();
		dealInfoDTO.setGoods_price_pa(basketDTO.getGoods_price_pa());
		dealInfoDTO.setGoods_price_pb(basketDTO.getGoods_price_pb());
		dealInfoDTO.setPoint_write(point_write);
		dealInfoDTO.setU_id(basketDTO.getU_id());
		dealInfoBiz.insertData2(dealInfoDTO, point_write, basketDTO, chk, u_id, deliveryDTO, payDTO);
		MemberDTO member = new MemberDTO();
		member.setPoint(point_write);
		member.setU_id(u_id);
		memberBiz.minusPoint(member);
		return "basket/test";
	}
	
	/**
	* @Method Name  : cart_User
	* @작성일   : 2012. 2. 6.
	* @작성자   : user
	* @변경이력  :
	* @Method 설명 : 주문조회물품정보
	* @param model
	* @param u_id
	* @return
	*/
	@RequestMapping(value="/cart_user", method=RequestMethod.GET)
	public String cart_User(Model model,@RequestParam("u_id")String u_id){
		tilesUrlBasedViewResolver.setTilesDefinitionName("default");
		model.addAttribute("data", basketBiz.getCartData(u_id));
		return "basket/cart_User";
	}
	
	@RequestMapping(value="/abcd", method=RequestMethod.GET)
	public String abcd(){
		tilesUrlBasedViewResolver.setTilesDefinitionName("default");
		return "basket/abcd";
	}
	
	
	
}
